<template>
  <div>
    <div id="main" style="width: 100%;min-height:400px;"></div>
  </div>
</template>

<script>
  import * as echarts from 'echarts';
  export default {
    props: ['printData','usageId'],
    data() {
      return {
        option: {},
        myChart: '',
      }
    },
    watch: {
      printData(val) {
        this.init_echarts()
      },
    },
    mounted() {
      this.init_echarts()
    },
    methods: {
      print() {
        let opt = {
          pixelRatio: 2
        }
        return this.myChart.getDataURL(opt);
      },
      init_echarts() {
        let xdata = []
        let ydata = []
        let legendData = []
        if(this.printData.datas){
          for(let key in this.printData.datas) {
            let obj = {
              name: this.printData.datas[key][0].metaName,
              type: 'line',
              smooth: true,
              data: []
            }
            legendData.push(this.printData.datas[key][0].metaName)
            this.printData.datas[key].forEach(item=>{
              xdata.push(this.$moment(item.time).format('YYYY-MM-DD'))
              obj.data.push([this.$moment(item.time).format('YYYY-MM-DD'),item.value])
            })
              ydata.push(obj)
          }
        }
        xdata = Array.from(new Set(xdata))
        // 基于准备好的dom，初始化echarts实例
        this.myChart = echarts.init(document.getElementById('main'));
        // 指定图表的配置项和数据
        let _this = this
        this.option = {
          // title: {
          //   text: 'ECharts 入门示例'
          // },
          grid: {
              show: true,
              top: 100,
            },
          tooltip: {
            trigger: 'item',
            formatter: function(data)
            {
                return data.seriesName + '<br/>日期：' +data.value[0]+ '<br/>指标：'+data.value[1];
             }
          },
          legend: {
            data: legendData
          },
          xAxis: {
          type: 'time',
           axisLabel: {
               interale: 0,
               formatter: function (value) {//在这里写你需要的时间格式
                 var t_date = new Date(value);
                 return [t_date.getFullYear(), t_date.getMonth() + 1, t_date.getDate()].join('-')
               }
             },
            // data: xdata
          },
          yAxis: {
          },
          series: ydata
        };
        // 使用刚指定的配置项和数据显示图表。
        this.myChart.setOption(this.option);
        let that = this
        setTimeout(function (){
        	    window.onresize = function () {
        	    	that.myChart.resize();
        	    }
        	},200)
      },
    },
  }
</script>

<style scoped>

</style>
